package com.xb.loan.util.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.Properties;

/**
 * 文件帮助类
 * 
 * @author heipn
 * 
 */
public class FileLoader {
    public static int TYPE_CLASSPATH = 1; //类路径 如：com/palmcommerce/util/log4j.properties

    public static int TYPE_FILEPATH = 0; //文件路径 如: c://test

    /**
     * 在类路径中读取配置文件。路径如下com/palmcommerce/util/log4j.properties
     * 
     * @param filePath
     * @return
     */
    public static Properties loadProByClassPath(String filePath) {
        Properties pro = null;
        URL url = FileLoader.class.getClassLoader().getResource(filePath);
        if (url != null) {

            Reader reader = null;

            try {
                reader = new InputStreamReader(FileLoader.class.getClassLoader().getResourceAsStream(filePath), "utf-8");
                pro = new Properties();
                pro.load(reader);
            } catch (Exception e) {
                System.out.println("load file fail : " + filePath);
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            System.out.println("properties file in class path not found: " + filePath);
        }
        return pro;
    }

    /**
     * 在文件路径中读取配置文件
     * 
     * @param filePath
     * @return
     */
    public static Properties loadProByFilePath(String filePath) {
        Properties pro = null;
        File file = new File(filePath);

        if (file.exists()) {
            InputStream in = null;

            try {
                in = new FileInputStream(file);
                pro = new Properties();
                pro.load(in);
            } catch (Exception e) {
                System.out.println("load file fail : " + filePath);
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            System.out.println("properties file in file path not found: " + filePath);
        }

        return pro;
    }

    /**
     * 获取文件流
     * @param filePath
     * @param pathType
     * @return
     */
    public static InputStream getInputStream(String filePath, int pathType) {
        InputStream in = null;
        if (FileLoader.TYPE_CLASSPATH == pathType) {
            in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
        } else {
            try {
                in = new FileInputStream(filePath);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        return in;
    }

    public static void main(String[] args) {
        Properties pro = loadProByClassPath("com/palmcommerce/util/log4j.properties");
        System.out.println(pro.get("log4j.rootLogger"));
    }

    /**
     * 根据文件路径和文件路径类型获得properties文件的Properties对象
     * @param filePath
     * @param pathType
     * @return
     */
    public static Properties loadPro(String filePath, int pathType) {
        if (pathType == TYPE_CLASSPATH)
            return loadProByClassPath(filePath);
        else
            return loadProByFilePath(filePath);
    }
}
